package com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.bean.Student;
import com.bean.Teacher;

public class TeacherDao extends DaoUtil {
	private PreparedStatement stat = null;
	private Connection conn = null;
	private ResultSet rs = null;
	
	//注册添加老师
	public boolean addTea(Teacher tea){
		boolean b = false;
		conn = this.getConnection();
		String  sql = "insert into teaUserInfo (teaUserID,teaName,teaEmail) values (?,?,?);";
		String  sql2 = "insert into teaLogin (teaUserID,teaName,teaPassword) values (?,?,?);";
		//System.out.println(sql);
		try {
			stat = conn.prepareStatement(sql);
			stat.setInt(1, tea.getTeaUserID());
			stat.setString(2, tea.getTeaName());
			stat.setString(3, tea.getTeaEmail());
			stat.execute();
			stat = conn.prepareStatement(sql2);
			stat.setInt(1, tea.getTeaUserID());
			stat.setString(2, tea.getTeaName());
			stat.setString(3, tea.getTeaPassword());
			return stat.execute();
			
		} catch (SQLException e) {
			e.printStackTrace();
			return b;
		}finally{
			closed();
		}
	}
	//注册查询
	public boolean testTea(Teacher tea){
		conn = this.getConnection();
		String sql = "select * from teaLogin where teaUserID = ?";
		try {
			stat = conn.prepareStatement(sql);
			stat.setInt(1,tea.getTeaUserID());
			rs = stat.executeQuery();
			if(rs.next()){
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		} finally{
			closed();
		}
	
		return false;
	}

	
	//查询老师登录
	public Teacher findTea(Teacher tea){
		conn = this.getConnection();
		String sql = "select * from teaLogin where teaUserID = ? and teaPassword = ?";
		try {
			stat = conn.prepareStatement(sql);
			stat.setInt(1,tea.getTeaUserID());
			stat.setString(2, tea.getTeaPassword().trim());
			rs = stat.executeQuery();
			if(rs.next()){
				return tea;
			}
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		} finally{
			closed();
		}
	
		return null;
	}

	//更新老师密码
	public int updateP(Teacher tea, String newP) {
		conn = this.getConnection();
		String sql = "UPDATE teaLogin SET teaPassword = ? WHERE teaUserID = ?";
		try {
			stat = conn.prepareStatement(sql);
			stat.setString(1,newP);
			stat.setInt(2, tea.getTeaUserID());
			return stat.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			
			e.printStackTrace();
			return 0;
		}finally {
			closed();
		}
		
	}
	//查找教师信息
	public Teacher findTeaInfo(int teaUserID){
		conn = this.getConnection();
		Teacher tea = new Teacher();
		//List list = new ArrayList();
		String sql = "select * from teauserinfo where teaUserID = ?";
		try {
			stat = conn.prepareStatement(sql);
			stat.setInt(1, teaUserID);
			rs = stat.executeQuery();
			while (rs.next()) {	
				tea.setTeaUserID(rs.getInt(1));
				tea.setTeaName(rs.getString(2));
				tea.setTeaEmail(rs.getString(3));
				
				System.out.println("教师列表更新成功！");
			}
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			
			e.printStackTrace();
			
		} finally {
			closed();
		}
		return tea;		
	}
	//教师个人信息更新
	public boolean updateteaself(Teacher tea){
		boolean b = false;
		conn = this.getConnection();
		/*Student stu2 = (Student) ServletActionContext.getRequest().getSession().getAttribute("stu");
		int stuid = stu2.getStuUserID();*/
		String sql = "update teauserinfo set teaName=?,teaEmail=? where teaUserID=?";
		String sql2 = "update tealogin set teaName=? where teaUserID=?";
		//System.out.println(sql);
		try {
			stat = conn.prepareStatement(sql);
			
			
			stat.setString(1, tea.getTeaName());
			stat.setString(2, tea.getTeaEmail());
			stat.setInt(3, tea.getTeaUserID());
			
			stat.execute();
			stat = conn.prepareStatement(sql2);
			stat.setString(1,tea.getTeaName());
			stat.setInt(2, tea.getTeaUserID());
			b=stat.execute();
			System.out.println("teaDao个人信息更新成功");
		} catch (SQLException e) {
			e.printStackTrace();
			
		}finally{
			this.closed();
		}
		return b;
	}
}
